(display "\n========================================\n")
(define (g . w)
    (display (cdr w)))

; (g 1 2 3 4 5)
; (newline)

; Reverse list
(define (reverse l)
    (define (iter items new-items)
        (if (null? items)
            new-items
            (iter (cdr items) (cons (car items) new-items))))
    (iter l ()))

; Find even numbers in list directly
(define (pick-even l)
    (define (iter items new-items)
        (cond ((null? items) new-items)
              ((= (remainder (car items) 2) 0)
                (iter (cdr items) (cons (car items) new-items)))
              (else
                (iter (cdr items) new-items))))
    (iter (reverse l) ()))

; Find odd numbers in list directly
(define (pick-odd l)
    (define (iter items new-items)
        (cond ((null? items) new-items)
              ((= (remainder (car items) 2) 1)
                (iter (cdr items) (cons (car items) new-items)))
              (else
                (iter (cdr items) new-items))))
    (iter (reverse l) ()))

(define (select l condition)
    (define (iter items new-items)
        (cond ((null? items) new-items)
              ((condition items)
                (iter (cdr items) (cons (car items) new-items)))
              (else
                (iter (cdr items) new-items))))
    (iter (reverse l) ()))

(define (pick-even l)
    (define (condition items)
        (= (remainder (car items) 2) 0))
    (select l condition))

(define (pick-odd l)
    (define (condition items)
        (= (remainder (car items) 2) 1))
    (select l condition))

(define (pick-odd l)
    (if (null? l)
        ()
        (if (odd? (car l))
            (cons (car l)
                  (pick-odd (cdr l)))
            (pick-odd (cdr l)))))

(define (pick-even l)
    (if (null? l)
        ()
        (if (even? (car l))
            (cons (car l)
                  (pick-even (cdr l)))
            (pick-even (cdr l)))))

(define (same-parity p . l)
    (if (= (remainder p 2) 0)
        (pick-even (cons p l))
        (pick-odd (cons p l))))

(define (same-parity sample . others)
    (filter (if (even? sample)
                even?
                odd?)
            (cons sample others)))

(display (same-parity 1 3 4 5 6 7 8 9 10 12))

(display "\n========================================\n")
